﻿USE [kb.net]
GO

/****** Object:  Trigger [dbo].[Activated_Archived]    Script Date: 7/9/2012 6:16:55 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		R. Brian Amesbury
-- Create date: 9 July 2012
-- Description:	Archives an Account
-- =============================================
CREATE TRIGGER [dbo].[Activated_Archived] 
   ON  [dbo].[Account_Activated]
   AFTER DELETE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	INSERT INTO [dbo].[Account_Activated_History]
		(
			ID,
			DateActivated,
			DateDeactivated
		)
		VALUES
		(
			(SELECT ID FROM deleted),
			(SELECT DateActivated FROM deleted),
			CURRENT_TIMESTAMP
		)
END

GO

USE [kb.net]
GO

/****** Object:  Trigger [dbo].[Login_Archive]    Script Date: 6/29/2012 5:45:41 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




-- =============================================
-- Author:		R. Brian Amesbury
-- Create date: 14 June 2012
-- Description:	Keeps a history of Account Login
-- =============================================
CREATE TRIGGER [dbo].[Login_Archive] 
   ON  [dbo].[Account_Login] 
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	DECLARE @ID int
	SET @ID = (SELECT ID FROM deleted)
	DECLARE @Login varchar(50)
	SET @Login = (SELECT Login FROM deleted)
	DECLARE @DateStart datetime2
	SET @DateStart = (SELECT ModifyDate FROM deleted)
	DECLARE @DateEnd datetime2
	SET @DateEnd = (SELECT ModifyDate FROM inserted)

	INSERT INTO [dbo].[Account_Login_History]
		(
			ID,
			Login,
			DateStart,
			DateEnd
		)
	VALUES
		(
			@ID,
			@Login,
			@DateStart,
			@DateEnd
		)
END




GO

USE [kb.net]
GO

/****** Object:  Trigger [dbo].[Name_Display_Archive]    Script Date: 6/29/2012 5:46:26 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





-- =============================================
-- Author:		R. Brian Amesbury
-- Create date: 26 June 2012
-- Description:	Keeps a history of Account Name_Display
-- =============================================
CREATE TRIGGER [dbo].[Name_Display_Archive] 
   ON  [dbo].[Account_Name_Display] 
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	DECLARE @ID int
	SET @ID = (SELECT ID FROM deleted)
	DECLARE @Name_Display varchar(100)
	SET @Name_Display = (SELECT Name_Display FROM deleted)
	DECLARE @DateStart datetime2
	SET @DateStart = (SELECT ModifyDate FROM deleted)
	DECLARE @DateEnd datetime2
	SET @DateEnd = (SELECT ModifyDate FROM inserted)

	INSERT INTO [dbo].[Account_Name_Display_History]
		(
			ID,
			Name_Display,
			DateStart,
			DateEnd
		)
	VALUES
		(
			@ID,
			@Name_Display,
			@DateStart,
			@DateEnd
		)
END





GO

USE [kb.net]
GO

/****** Object:  Trigger [dbo].[Name_First_Archive]    Script Date: 6/29/2012 5:48:42 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




-- =============================================
-- Author:		R. Brian Amesbury
-- Create date: 14 June 2012
-- Description:	Keeps a history of Account Name_First
-- =============================================
CREATE TRIGGER [dbo].[Name_First_Archive] 
   ON  [dbo].[Account_Name_First] 
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	DECLARE @ID int
	SET @ID = (SELECT ID FROM deleted)
	DECLARE @Name_First varchar(50)
	SET @Name_First = (SELECT Name_First FROM deleted)
	DECLARE @DateStart datetime2
	SET @DateStart = (SELECT ModifyDate FROM deleted)
	DECLARE @DateEnd datetime2
	SET @DateEnd = (SELECT ModifyDate FROM inserted)

	INSERT INTO [dbo].[Account_Name_First_History]
		(
			ID,
			Name_First,
			DateStart,
			DateEnd
		)
	VALUES
		(
			@ID,
			@Name_First,
			@DateStart,
			@DateEnd
		)
END




GO

USE [kb.net]
GO

/****** Object:  Trigger [dbo].[Name_Last_Archive]    Script Date: 6/29/2012 5:49:18 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





-- =============================================
-- Author:		R. Brian Amesbury
-- Create date: 14 June 2012
-- Description:	Keeps a history of Account Name_Last
-- =============================================
CREATE TRIGGER [dbo].[Name_Last_Archive] 
   ON  [dbo].[Account_Name_Last] 
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	DECLARE @ID int
	SET @ID = (SELECT ID FROM deleted)
	DECLARE @Name_Last varchar(50)
	SET @Name_Last = (SELECT Name_Last FROM deleted)
	DECLARE @DateStart datetime2
	SET @DateStart = (SELECT ModifyDate FROM deleted)
	DECLARE @DateEnd datetime2
	SET @DateEnd = (SELECT ModifyDate FROM inserted)

	INSERT INTO [dbo].[Account_Name_Last_History]
		(
			ID,
			Name_Last,
			DateStart,
			DateEnd
		)
	VALUES
		(
			@ID,
			@Name_Last,
			@DateStart,
			@DateEnd
		)
END





GO

USE [kb.net]
GO

/****** Object:  Trigger [dbo].[Password_Archive]    Script Date: 6/29/2012 5:50:00 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





-- =============================================
-- Author:		R. Brian Amesbury
-- Create date: 14 June 2012
-- Description:	Keeps a history of Account Password
-- =============================================
CREATE TRIGGER [dbo].[Password_Archive] 
   ON  [dbo].[Account_Password] 
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	DECLARE @ID int
	SET @ID = (SELECT ID FROM deleted)
	DECLARE @Password varchar(50)
	SET @Password = (SELECT Password FROM deleted)
	DECLARE @DateStart datetime2
	SET @DateStart = (SELECT ModifyDate FROM deleted)
	DECLARE @DateEnd datetime2
	SET @DateEnd = (SELECT ModifyDate FROM inserted)

	INSERT INTO [dbo].[Account_Password_History]
		(
			ID,
			Password,
			DateStart,
			DateEnd
		)
	VALUES
		(
			@ID,
			@Password,
			@DateStart,
			@DateEnd
		)
END





GO

USE [kb.net]
GO

/****** Object:  Trigger [dbo].[PIN_Archive]    Script Date: 6/29/2012 5:50:39 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





-- =============================================
-- Author:		R. Brian Amesbury
-- Create date: 14 June 2012
-- Description:	Keeps a history of Account PIN
-- =============================================
CREATE TRIGGER [dbo].[PIN_Archive] 
   ON  [dbo].[Account_PIN] 
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	DECLARE @ID int
	SET @ID = (SELECT ID FROM deleted)
	DECLARE @PIN varchar(50)
	SET @PIN = (SELECT PIN FROM deleted)
	DECLARE @DateStart datetime2
	SET @DateStart = (SELECT ModifyDate FROM deleted)
	DECLARE @DateEnd datetime2
	SET @DateEnd = (SELECT ModifyDate FROM inserted)

	INSERT INTO [dbo].[Account_PIN_History]
		(
			ID,
			PIN,
			DateStart,
			DateEnd
		)
	VALUES
		(
			@ID,
			@PIN,
			@DateStart,
			@DateEnd
		)
END





GO

USE [kb.net]
GO

/****** Object:  Trigger [dbo].[Question_Archive]    Script Date: 6/29/2012 5:51:57 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





-- =============================================
-- Author:		R. Brian Amesbury
-- Create date: 14 June 2012
-- Description:	Keeps a history of Account Security Question
-- =============================================
CREATE TRIGGER [dbo].[Question_Archive] 
   ON  [dbo].[Account_Security_Question] 
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
	DECLARE @ID int
	SET @ID = (SELECT ID FROM deleted)
	DECLARE @Question varchar(50)
	SET @Question = (SELECT Question FROM deleted)
	DECLARE @DateStart datetime2
	SET @DateStart = (SELECT ModifyDate FROM deleted)
	DECLARE @DateEnd datetime2
	SET @DateEnd = (SELECT ModifyDate FROM inserted)

	INSERT INTO [dbo].[Account_Security_Question_History]
		(
			ID,
			Question,
			DateStart,
			DateEnd
		)
	VALUES
		(
			@ID,
			@Question,
			@DateStart,
			@DateEnd
		)
END





GO



